using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace SpaceWolf.GameLogic
{
    public class Commander
    {
        public string name;
        public int planet;
        public string sPlanetName;
        public float credits;
        public int galaxy_number;
        
        //public List<stock_item> Cargo = new List<stock_item>();
        
        public int[] current_cargo;
        public int legal_status;
        public byte[] station_stock;
        //public int market_rnd=20;           //Neizvestno , zachem ono nado
        public int score;
        public bool saved;
        public Ship ship;
        public bool IsGameOver = false;


        private ArrayList arrMissions = new ArrayList();
        public ArrayList Missions
        {
            get
            {
                return this.arrMissions;
            }
        }

        /// <summary>
        /// How many missions player have
        /// </summary>
        public int MissionsCount
        {
            get
            {
                int iCount=0;
                foreach (Mission MissionToDo in arrMissions)
                {
                    if (!MissionToDo.DoneStatus)
                    {
                        iCount++;
                    }
                }
                return iCount;
            }
        }

        /// <summary>
        /// Current life time in ms. This holds the time how long we currently
        /// live with our rocket. Used to countdown and zooming into the rocket.
        /// </summary>
        public static float lifeTimeMs = 0;

        /// <summary>
        /// Creates new commander class include his ship
        /// </summary>
        /// <param name="name"></param>
        /// <param name="mission"></param>
        /// <param name="credits"></param>
        /// <param name="galaxy_number"></param>
        /// <param name="planet"></param>
        /// <param name="current_cargo"></param>
        /// <param name="legal_status"></param>
        /// <param name="score"></param>
        /// <param name="saved"></param>
        public Commander(string name,  float credits, int galaxy_number, int planet,string sPlanetName,
            int[] current_cargo,int legal_status,int score,bool saved)
        {
            this.name = name;
            this.credits = credits;
            this.galaxy_number = galaxy_number;
            this.planet = planet;
            this.current_cargo = current_cargo;
            this.legal_status = legal_status;
            this.score = score;
            this.saved = saved;
            this.sPlanetName = sPlanetName;
            
            SetShipForCommander();
        }

        public void SetShipForCommander()
        {
            Matrix shipWorld = Matrix.CreateScale(0.1f);//*Matrix.CreateRotationX(MathHelper.ToRadians(90))
            ship = new Ship(ShipsData.CobraMK3_Data, ShipOwner.Player, Vector3.Zero, shipWorld, Vector3.Zero);
            ship.CurrentLaser = Armory.MilitaryLaser;
            ship.Coolant = Armory.Coolant4;
            //ship = new Ship(40, 25, 8, 70,255,30);
        }

        float virtualTurningAmount = 0.0f;
        float virtualClimbingAmount = 0.0f;
        float virtualRotationAmount = 0;
        Vector3 shipForce = Vector3.Zero;
        private float LeftRightRot = 0;
        private float UpDownRot = 0;
        private float ZAxisRot = 0;
        /// <summary>
        /// Max rotation per second we use for our car.
        /// </summary>
        public const float MaxRotationPerSec = 1.3f;//0.95f

        public void Update()
        {
            float moveFactor = BaseGame.MoveFactorPerSecond;
            if (moveFactor < 0.001f)
                moveFactor = 0.001f;
            if (moveFactor > 0.5f)
                moveFactor = 0.5f;
            moveFactor = 0.03f;
            //// First handle rotations (reduce last value)
            //ship.flight_roll *=  0.95f;//0.75f;
            //ship.flight_climb *= 0.75f;
            if (InputManager.IsActionPressed(Action.Afterburners))
            {
                ship.IsAfterburnesON = true;
            }
            else
            {
                ship.IsAfterburnesON = false;
            }

            if (InputManager.IsActionPressed(Action.IncFlightTurn) ||
                    InputManager.IsActionPressed(Action.DecFrlightTurn))
            {
                if (InputManager.IsActionPressed(Action.IncFlightTurn))
                    ship.Turn -= MaxRotationPerSec * moveFactor / 2.5f;
                else
                    ship.Turn += MaxRotationPerSec * moveFactor / 2.5f;
            }
            else
            {
                ship.Turn= 0;
            }

            if (InputManager.IsActionPressed(Action.IncFlightRoll) ||
                  InputManager.IsActionPressed(Action.DecFrlightRoll))
            {
                if (InputManager.IsActionPressed(Action.IncFlightRoll))
                    ship.Roll -= MaxRotationPerSec * moveFactor / 1.5f;
                else
                    ship.Roll+= MaxRotationPerSec * moveFactor / 1.5f;
            }
            else
            {
                ship.Roll = 0;
            }

            if (InputManager.IsActionPressed(Action.IncFlighClimb) ||
                InputManager.IsActionPressed(Action.DecFlighClimb))
            {
                if (InputManager.IsActionPressed(Action.DecFlighClimb))
                    ship.Climb += MaxRotationPerSec * moveFactor / 0.5f;
                else
                    ship.Climb-= MaxRotationPerSec * moveFactor / 0.5f;
            }
            else
            {
                ship.Climb = 0;
            }
            // Smooth over 200ms
            //virtualRotationAmount += flight_roll;
            // / 0.200f;
            if (InputManager.MouseXMovement != 0 && InputManager.MouseRightButtonPressed)
                ship.Roll -=
                    (InputManager.MouseXMovement / 15.0f) * MaxRotationPerSec * moveFactor / 0.5f;
            else if (InputManager.MouseXMovement != 0)
                ship.Turn-=
                    (InputManager.MouseXMovement / 15.0f) * MaxRotationPerSec * moveFactor / 0.5f;
            if (InputManager.MouseYMovement != 0)
                ship.Climb +=
                    (InputManager.MouseYMovement / 15.0f) * MaxRotationPerSec * moveFactor / 0.5f;

            //shipDir = Vector3.TransformNormal(shipDir,
            float maxRot = MaxRotationPerSec * moveFactor * 1.25f; // * 1;
            if (ship.Roll> maxRot)
                ship.Roll= maxRot;
            if (ship.Roll< -maxRot)
                ship.Roll= -maxRot;
             virtualRotationAmount += ship.Roll;
             ZAxisRot=(ship.Roll+ virtualRotationAmount) * moveFactor / 0.225f;

             virtualRotationAmount -= ZAxisRot;

            // Limit rotation change to MaxRotationPerSec * 1.5 (usually for mouse)
            if (ship.Turn > maxRot)
                ship.Turn = maxRot;
            if (ship.Turn < -maxRot)
                ship.Turn = -maxRot;

            virtualTurningAmount += ship.Turn;
            LeftRightRot = (ship.Turn + virtualTurningAmount) * moveFactor / 0.225f;

            virtualTurningAmount -= LeftRightRot;

            // Smooth over 200ms
            ship.Climb *= 1.0f + (ship.Speed - 10) / 100.0f;

            //virtualClimbingAmount += flight_climb;

            // / 0.200f;

            if (ship.Climb > maxRot)
                ship.Climb = maxRot;
            if (ship.Climb < -maxRot)
                ship.Climb = -maxRot;

            virtualClimbingAmount += ship.Climb;

            UpDownRot = (ship.Climb + virtualClimbingAmount) * moveFactor / 0.225f;

            virtualClimbingAmount -= UpDownRot;

            //ship.Rotations = new Vector3(0.01f, 0.01f, 0);
            ship.Rotations = new Vector3(LeftRightRot, UpDownRot, ZAxisRot);
            ship.Location = Vector3.Zero;
            
        }

        public void AddMission(Mission mission)
        {
            foreach (Mission MissionToDo in arrMissions )
            {
                if (mission == MissionToDo)
                {
                    break;
                }
            }
            arrMissions.Add(mission);
        }

        public bool CheckMission(Mission mission)
        {
            bool result = false;
            foreach (Mission MissionToDo in arrMissions)
            {
                if (mission.Description == MissionToDo.Description)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        public int total_cargo()
        {
            int i;
            int cargo_held;

            cargo_held = 0;
            for (i = 0; i < 17; i++)
            {
                if (current_cargo[i] > 0) 
                {
                    cargo_held += current_cargo[i];
                }
            }

            return cargo_held;
        }

        public int carrying_contraband()
        {
            int SLAVES = 3;
            int NARCOTICS = 6;
            int FIREARMS = 10;
            return (current_cargo[SLAVES] + current_cargo[NARCOTICS]) * 2 +
                        current_cargo[FIREARMS];
        }

    }
}

